package com.lwc.cfdns.utils.file;

import com.lwc.cfdns.config.AppConfig;
import com.lwc.cfdns.exception.BadException;
import com.lwc.cfdns.utils.file.upload.AliyunOssFileUpload;
import com.lwc.cfdns.utils.file.upload.LocalFileUpload;
import org.springframework.web.multipart.MultipartFile;

/**
 * 上传驱动
 *
 * @author luwc
 * @title StorageUtils
 * @description
 * @time 2023/6/4 11:19
 * @version: 1.0
 */
public class StorageUtils {
    /**
     * 根据配置返驱动
     *
     * @param
     * @return IFileUpload
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/9/19 17:17:00
     */
    private static IFileUpload getStorage() {
        IFileUpload storage = null;
        switch (AppConfig.getAttachmentStorage()) {
            case "local":
                storage = new LocalFileUpload();
                break;
            case "aliyunoss":
                storage = new AliyunOssFileUpload();
                break;
            default:
                throw new BadException("附件驱动错误!");
        }
        return storage;
    }

    /**
     * 上传文件
     *
     * @param file
     * @return String
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/6/4 11:16
     */
    public static String uploadFile(MultipartFile file) {
        IFileUpload localFileUploadUtils = getStorage();
        return localFileUploadUtils.getUploadDomain() + localFileUploadUtils.upload(file, MimeTypeUtils.FILE_EXTENSION);
    }

    /**
     * 上传图片
     *
     * @param file
     * @return String
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/6/4 11:16
     */
    public static String uploadImage(MultipartFile file) {
        IFileUpload localFileUploadUtils = getStorage();
        return localFileUploadUtils.getUploadDomain() + localFileUploadUtils.upload(file, MimeTypeUtils.IMAGE_EXTENSION);
    }


    /**
     * 上传文件
     *
     * @param file
     * @return String
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/6/4 11:16
     */
    public static String upload(MultipartFile file) {
        IFileUpload localFileUploadUtils = getStorage();
        return localFileUploadUtils.getUploadDomain() + localFileUploadUtils.upload(file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
    }
}
